﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using DAL;
using System.Web.Script.Serialization;
using System.Data.SqlClient;

namespace 公司CRM客户关系系统.CustomerService
{
    /// <summary>
    /// CustomServicesWeb 的摘要说明
    /// </summary>
    public class CustomServicesWeb : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string action=context.Request["action"];
            if(action=="paging")
            {//查询全部 
                int pageIndex = int.Parse(context.Request["pageIndex"]);
                int pageSize = int.Parse(context.Request["pageSize"]);
                PagingList<V_ServiceType> list = BaseDAL.Paging<V_ServiceType>(pageSize, pageIndex, false);
                //返回结果
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(new { total = list.RecordCount, data = list });
                context.Response.Write(json);
            }
            else if (action == "find")
            {//找到服务表的id
                int CSID = int.Parse(context.Request["CSID"]);//服务处理ID
                //查询
                V_ServiceType s = BaseDAL.Find<V_ServiceType>(CSID);
                //转为json
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(s);
                //输出结果
                context.Response.Write(json);
            }
            else if (action == "chuli")
            {//服务处理
                int CSID = int.Parse(context.Request["CSID"]);//服务处理ID
                string CSDeal = context.Request["CSDeal"];//服务处理内容
                CustomServices CS = new CustomServices();
                CS.CSID = CSID;
                CS.CSDeal = CSDeal;
                CS.CSDealDate = DateTime.Now;
                //从数据库中查出来的，不需要修改
                CustomServices Cus=BaseDAL.Find<CustomServices>(CSID);
                CS.STID =Cus.STID;
                CS.CusID =Cus.CusID;
                CS.CSTitle =Cus.CSTitle;
                CS.CSState =Convert.ToInt32(3);
                CS.CSDesc =Cus.CSDesc;
                CS.CSCreateID =Cus.CSCreateID;
                CS.CSCreateDate =Cus.CSCreateDate;
                CS.CSDueID =Cus.CSDueID;
                CS.CSDueDate =Cus.CSDueDate;
                CS.CSResult =Cus.CSResult;
                CS.CSSatisfy =Cus.CSSatisfy;
                CS.CSName = Cus.CSName;
                //得到增加的数字
                int r = BaseDAL.Update(CS);
                context.Response.Write(r);
            }
            else if (action == "fan")
            {//服务反馈
                int CSID = int.Parse(context.Request["CSID"]);//服务处理ID
                string CSResult = context.Request["CSResult"];//处理结果
                int CSSatisfy = int.Parse(context.Request["CSSatisfy"]);//满意度
                CustomServices CS = new CustomServices();
                CS.CSID = CSID;
                CS.CSResult = CSResult;
                CS.CSSatisfy = CSSatisfy;
                //从数据库中查出来的，不需要修改
                CustomServices Cus = BaseDAL.Find<CustomServices>(CSID);
                CS.STID = Cus.STID;
                CS.CusID = Cus.CusID;
                CS.CSTitle = Cus.CSTitle;
                CS.CSState = Cus.CSState;
                CS.CSDesc = Cus.CSDesc;
                CS.CSCreateID = Cus.CSCreateID;
                CS.CSCreateDate = Cus.CSCreateDate;
                CS.CSDueID = Cus.CSDueID;
                CS.CSDueDate = Cus.CSDueDate;
                CS.CSDeal = Cus.CSDeal;
                CS.CSDealDate = Cus.CSDealDate;
                CS.CSName = Cus.CSName;
                //得到增加的数字
                int r = BaseDAL.Update(CS);
                context.Response.Write(r);
            }
            else if (action == "select")
            {//有条件的服务归档查询
                int pageIndex = int.Parse(context.Request["pageIndex"]);
                int pageSize = int.Parse(context.Request["pageSize"]);
                //得到传过来的值
                string CSName = context.Request["CSName"];
                string STName = context.Request["STName"];
                //进行分页查询
                //执行查询
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("@CSName", CSName);
                pars[1] = new SqlParameter("@STName", STName);

                string whenStr = "CSName like '%'+@CSName+'%' and STName like '%'+@STName+'%'";
                PagingList<V_ServiceType> list = BaseDAL.Paging<V_ServiceType>(pageSize, pageIndex, whenStr, pars, false);
                //返回结果
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(new { total = list.RecordCount, data = list });
                context.Response.Write(json);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}